package com.zhupanlin.config;

import com.zhupanlin.filter.KaptchFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/12/15 22:06
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        userDetailsManager.createUser(User.withUsername("user").password("{noop}123").roles("admin").build());
        return userDetailsManager;
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public KaptchFilter kaptchFilter() throws Exception {
        KaptchFilter kaptchFilter = new KaptchFilter();
        kaptchFilter.setFilterProcessesUrl("/doLogin");
        kaptchFilter.setUsernameParameter("username");
        kaptchFilter.setPasswordParameter("password");
        kaptchFilter.setKaptchaParameter("kaptcha");

        // 指定认证管理器
        kaptchFilter.setAuthenticationManager(authenticationManagerBean());
        // 指定认证成功处理
        kaptchFilter.setAuthenticationSuccessHandler(((request, response, authentication) -> {
            response.sendRedirect("/index.html");
        }));
        // 指定认证失败处理
        kaptchFilter.setAuthenticationFailureHandler((request, response, exception) -> {
            response.sendRedirect("/login.html");
        });
        return kaptchFilter;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests()
                .mvcMatchers("/vc.jpg").permitAll()
                .mvcMatchers("/login.html").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .and()
                .logout()
                .logoutUrl("logout")
                .and()
                .csrf().disable();

        http.addFilterAt(kaptchFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}
